package org.hackystat.timeline.resource.timelinedata;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import org.hackystat.timeline.service.FileDispatcher;
import org.restlet.Context;
import org.restlet.data.MediaType;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.resource.StringRepresentation;
import org.restlet.resource.Variant;

/**
 * Represents a resource of a timeline data ready to be mapped onto a timeline band. Can be accessed
 * by URI:
 * 
 * @author Alexey R. Olkov
 * 
 */
public class TimelineDataResource extends Resource {

  String userKey, projectName, sensorDataType, startTimestamp, endTimestamp;

  static String hostName;;
  Request request = null;

  /**
   * Overriden constructor.
   * 
   * @param context Context
   * @param request Request
   * @param response Response
   */
  public TimelineDataResource(Context context, Request request, Response response) {
    super(context, request, response);

    System.out.println("TIMELINE RESOURCE IS ACTIVE");

    // Extract all data
    this.request = request;
    this.userKey = (String) request.getAttributes().get("userkey");
    this.projectName = (String) request.getAttributes().get("projectname");
    this.sensorDataType = (String) request.getAttributes().get("sensordatatype");
    this.startTimestamp = (String) request.getAttributes().get("start_timestamp");
    this.endTimestamp = (String) request.getAttributes().get("end_timestamp");

    // Set Variant
    this.getVariants().add(new Variant(MediaType.TEXT_XML));

  };

  /**
   * Returns the representation of the TimelineData resource.
   * 
   * @param variant The representational variant requested.
   * @return The representation.
   */
  @Override
  public Representation getRepresentation(Variant variant) {
   System.out.println("Getting Timeline Representation");
    // FROM HERE THE DATA WILL BE COLLECTED
    
    FileDispatcher f = new FileDispatcher(this.hostName, this.request);
    File file = new File("www\\TimeLineTest\\TimeLineTest.html");

    Representation result = null;
    try {
      StringBuffer fileData = new StringBuffer(1000);
      BufferedReader reader = new BufferedReader(new FileReader(file));
      char[] buf = new char[1024];
      int numRead = 0;
      while ((numRead = reader.read(buf)) != -1) {
        String readData = String.valueOf(buf, 0, numRead);
        fileData.append(readData);
        buf = new char[1024];
      }
      reader.close();
      System.out.println("READ SUCCESSFULLY");
      result = new StringRepresentation(fileData.toString());
      result.setMediaType(MediaType.TEXT_HTML);

      result = f.retrieveData();
      if (result == null) {
        System.out.println("NOTHING IS RETURNED!");
      }
      else {
        System.out.println("Representation retrieved!");
        result.setTransient(true);
        result.setMediaType(MediaType.TEXT_XML);
      }

    }
    catch (Exception e) {
      System.out.println("ERROR RETRIEVING DATA:" + e.getMessage());
      return result;
    }
    return result;
  }

  /**
   * Returns a projectName.
   * 
   * @return projectName
   */
  public String getProjectName() {
    return this.projectName;
  };

  /**
   * Returns a userkey.
   * 
   * @return UserKey
   */
  public String getUserKey() {
    return this.userKey;
  }

  /**
   * Returns a sensorDataType.
   * 
   * @return sensorDataType
   */
  public String getSensorDataType() {
    return this.sensorDataType;
  }

  /**
   * Returns a startTimestamp.
   * 
   * @return startTimestamp
   */
  public String getStartTimestamp() {
    return this.startTimestamp;
  }

  /**
   * Returns a endTimestamp.
   * 
   * @return endTimestamp
   */
  public String getEndTimestamp() {
    return this.endTimestamp;
  }

  /**
   * Returns a host name.
   * 
   * @return host name
   */
  public String getHostName() {
    return this.hostName;
  }

  /**
   * Sets the host name.
   * 
   * @param name host name.
   */
  public static void setHostName(String name) {
    hostName = name;
  }
}
